package com.company.service.impl;


import com.company.dao.OrderSettingDao;
import com.company.pojo.OrderSetting;
import com.company.service.OrderSettingService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fu xiao liu
 * @date 2021-08-10 17:02
 * 预约订单管理（设置预约日期以及人数）
 */

@DubboService(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(ArrayList<OrderSetting> arrayList) {
        if(arrayList!=null && arrayList.size() > 0){
            for (OrderSetting orderSetting : arrayList) {
                //判断日期是否已经进行预约
                long countByOrderDate = orderSettingDao.findCountByOrderDate(
                        orderSetting.getOrderDate());
                if(countByOrderDate > 0){
                    //已经进行了预约设置，执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //没有进行预约设置，执行插入操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        // 2021-8-1
        String begin = date + "-1";
        // 2021-8-31
        String end = date + "-31";
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //调用Dao层 ，根据日期访问查询预约设置
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);

        List<Map> result = new ArrayList<>();
        if (list != null && list.size() > 0){
            for (OrderSetting order : list) {
                Map<String, Object> m = new HashMap<>();
                m.put("date",order.getOrderDate().getDate());
                m.put("number",order.getNumber());
                m.put("reservations",order.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            orderSettingDao.add(orderSetting);
        }

    }
}
